package com.shunwang.tools.httpclientsupport.client;

import java.io.IOException;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;

public abstract class AbstractHttpClientNode {

	private final HttpClientInfo info;
	private PostMethod method;
	private HttpClient client;
	private String encode = "UTF-8";
	
	public AbstractHttpClientNode(HttpClientInfo clientInfo){
		this.info = clientInfo;
		if(info.getEncode() != null){
			this.encode = info.getEncode();
		}
		create();
	}
	
	public void disconnect(){
		method.releaseConnection();
	}
	
	public void connection(){
	}
	
	public abstract String sendPost(NameValuePair[] pair) throws HttpException,IOException;
	
	public abstract String sendPost(NameValuePair pair) throws HttpException,IOException;
	
	public abstract String sendPost(List<NameValuePair> pair) throws HttpException,IOException;
	
	protected AbstractHttpClientNode create(){
		HttpClientParams p = new HttpClientParams();
        p.setContentCharset(getEncode());
		this.client = new HttpClient(p);
		client.getHostConfiguration().setHost(info.getHost(),info.getPort(), info.getProtocol());
		return this;
	}

	public HttpClientInfo getInfo() {
		return info;
	}

	public HttpClient getClient() {
		return client;
	}
	public String getEncode() {
		return encode;
	}

	public void setEncode(String encode) {
		this.encode = encode;
	}
	
	
	
}
